home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 20 code / Scripting the Finder / Finder Tricks / NextEvent.cp < prev    next >
Encoding:
Text File  |  1994-10-04  |  3.0 KB  |  117 lines  |  [TEXT/MMCC]

  1. /*================================================================================
  2.     NextEvent.c
  3.         
  4.     ©1991 Greg Anderson
  5.     greggor@apple.com
  6.     
  7.     This code used to abstract GetNextEvent vs. WaitNextEvent, but now
  8.     it always calls WaitNextEvent, and therefore is largely obsolete.
  9.     
  10.     IMPORTANT:        NextEvent returns the result of IsDialogEvent, _not_
  11.                     eventAvailable.  Your code should not call IsDialogEvent,
  12.                     because NextEvent mauls the event record in ways that
  13.                     make IsDialogEvent _very_ unhappy.
  14. ================================================================================*/
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <Types.h>
  18. #include <OSUtils.h>
  19. #include <Events.h>
  20. #include <Resources.h>
  21. #include <Dialogs.h>
  22. #include <Controls.h>
  23. #include <Traps.h>
  24. #include <Processes.h>
  25.  
  26. #include "NextEvent.h"
  27. #include "MacUtilities.h"
  28.  
  29. /*----------------------------------------------------------------------
  30. // InForeground
  31. //
  32. // Return 'true' if this application is in the foreground
  33. ----------------------------------------------------------------------*/
  34. Boolean InForeground()
  35. {
  36.     ProcessSerialNumber frontPSN;
  37.     ProcessSerialNumber currentPSN;
  38.     Boolean isSame;
  39.     
  40.     GetFrontProcess(&frontPSN);
  41.     GetCurrentProcess(¤tPSN);
  42.     SameProcess(&frontPSN, ¤tPSN, &isSame);
  43.     
  44.     return isSame;
  45. }
  46.  
  47. /*----------------------------------------------------------------------
  48. // NextEvent
  49. //
  50. // Call GetNextEvent or WaitNextEvent.
  51. //    
  52. // Does _not_ return eventAvailable (like GetNextEvent and
  53. // WaitNextEvent); instead, returns isDialogEvent.
  54. ----------------------------------------------------------------------*/
  55. Boolean NextEvent( short eventMask, EventRecord* event, long sleep, RgnHandle mouseRgn)
  56. {
  57.     Boolean            eventAvailable;
  58.     Boolean            isDialogEvent;
  59.     GrafPtr            savePort;
  60.     Point            mouseLoc;
  61.     long            ticky;
  62.     
  63.     /*
  64.     // Call WaitNextEvent()
  65.     */
  66.     eventAvailable = WaitNextEvent( eventMask, event, sleep, mouseRgn );
  67.  
  68.     /*
  69.     // We must call 'IsDialogEvent' _before_ we maul the
  70.     // event record.  Also, IsDialogEvent will not always
  71.     // perform correctly if there is no frontmost window,
  72.     // so we check for that case explicitly (this is an
  73.     // undocumented problem with the Macintosh Toolbox).
  74.     */
  75.     isDialogEvent = false;
  76.     if( FrontWindow() != nil )
  77.     {
  78.         isDialogEvent = IsDialogEvent( event );
  79.     }
  80.     /*
  81.     // If the current event is NOT a dialog event, then
  82.     // maul the event record to make it easier to parse
  83.     // in the main event switch
  84.     */
  85.     if( !isDialogEvent )
  86.     {
  87.         /*
  88.         // Decode suspend, resume and mouse-moved events
  89.         */
  90.         if( event->what == app4Evt )
  91.         {
  92.             if( IsSuspend(event) )
  93.             {
  94.                 event->what = suspendEvt;
  95.             }
  96.             if( IsResume(event) )
  97.             {
  98.                 event->what = resumeEvt;
  99.             }
  100.             if( IsMouseMoved(event) )
  101.                 event->what = mouseMovedEvt;
  102.         }
  103.         /*
  104.         // If the event is an activate or deactivate event, check
  105.         // the event modifiers and change event->what to deactivateEvt
  106.         // if appropriate
  107.         */
  108.         if( event->what == activateEvt )
  109.         {
  110.             if( (event->modifiers & activeFlag) == 0 )
  111.                 event->what = deactivateEvt;
  112.         }
  113.     }
  114.     
  115.     return isDialogEvent;
  116. }
  117.